<?php

class Model_User extends Zend_Db_Table_Abstract
{
    protected $_primary = 'id';

    protected function _setupTableName()
    {
       $this->_name = Zend_Registry::get('config')
                        ->database
                        ->authAdapter
                        ->tableName;

       parent::_setupTableName();
    }

    public function insert(array $data)
    {
        // Add custom logic here
        unset($data['password_confirmation']);
        unset($data['captcha']);
        unset($data['submit']);
        unset($data['MAX_FILE_SIZE']);

        $data['password'] = sha1($data['password']);
        $data['email_confirmation_key'] = sha1(microtime());
        $data['created_on'] = new Zend_Db_Expr('NOW()');

        $id = parent::insert($data);

        $mail = new Model_Mail_RegistrationConfirmation(array(
                    'to' => $data['email'],
                    'toName' => $data['username'],
                    'confirmationKey' => $data['email_confirmation_key'],
                    )
                );
        $mail->send();

        return $id;
    }

    public function updateConfirmedUser(array $data)
    {
        $where = $this
                    ->getAdapter()
                    ->quoteInto('email_confirmation_key = ?', $data['email_confirmation_key']);

        return parent::update($data, $where);
    }
}
